/*******************************************************************
 * 外语翻译平台(translate) 
 *
 * 成都一亿信息技术公司 版权所有
 * 
 * Copyright (C) 2010 Transer Information Technology Co., Ltd.
 *
 * @Version : 1.0  2010-12-26 下午08:41:24
 *
 * UserLoginAction.java 
 *
 *******************************************************************/
package com.transer.web.action.passport;

import org.apache.commons.lang.StringUtils;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.transer.console.ConsoleConstants;
import com.transer.constant.Constants;
import com.transer.job.AddLoginLogJob;
import com.transer.job.QueryProfileLanguageJob;
import com.transer.persistance.bean.LoginLog;
import com.transer.persistance.bean.User;
import com.transer.persistance.dao.ILogDao;
import com.transer.persistance.dao.IUserDao;
import com.transer.utils.JobQueueOB;
import com.transer.utils.MD5;
import com.transer.web.action.AuthCodeActionSupport;

/**
 * class defination are there
 * 
 * @author Mark
 * 
 * @version 1.0 ywengineer@gmail.com
 * 
 */
public class UserLoginAction extends AuthCodeActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1175143772476314852L;
	// 用户操作
	protected IUserDao userDao;
	// 日志操作
	protected ILogDao logDao;
	// 用户名
	protected String username;
	// 密码
	protected String passwd;
	// 跳转路径
	protected String jumpurl;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		User user = userDao.loginPassport(username);
		// 不存在用户
		// 没有激活
		// 密码不正确
		if (user == null || !user.isActive() || !user.getPassword().equals(MD5.getMD5(passwd))) {
			addFieldError("username", getText("error.login"));
			return Action.INPUT;
		}
		// 登陆成功
		ActionContext.getContext().getSession().put(Constants.SESSION_USER, user);
		// 查询语言配对信息
		JobQueueOB.getInstance().addJob(ConsoleConstants.QUEUE_INDEX_SAVE_USER, new QueryProfileLanguageJob(userDao, user));
		// 添加登陆日志
		JobQueueOB.getInstance().addJob(ConsoleConstants.QUEUE_INDEX_MAIL, new AddLoginLogJob(logDao, new LoginLog(user)));
		// 如果有跳转的页面
		if (StringUtils.isNotEmpty(jumpurl)) {
			return Action.NONE;
		}
		// 返回成功
		return Action.SUCCESS;
	}

	public IUserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	/**
	 * @return the logDao
	 */
	public ILogDao getLogDao() {
		return logDao;
	}

	/**
	 * @param logDao
	 *            the logDao to set
	 */
	public void setLogDao(ILogDao logDao) {
		this.logDao = logDao;
	}

	/**
	 * @return the jumpurl
	 */
	public String getJumpurl() {
		return jumpurl;
	}

	/**
	 * @param jumpurl
	 *            the jumpurl to set
	 */
	public void setJumpurl(String jumpurl) {
		this.jumpurl = jumpurl;
	}

}
